var storage = {
    get: function (key) {
        return localStorage.getItem(key);
    },
    set: function (key, value) {
        localStorage.setItem(key, value);
    }
};

// const api = 'http://127.0.0.1:7002/api';
const api = 'https://www.tbplas.net/app_adminTbplas';
const loginKey = 'tbplas_token';

const isPublicPage = () => {
    const publicPaths = ['login.html', 'forgot-password.html', 'register.html', 'email-sender.html', 'user.html'];
    return publicPaths.some(path => window.location.pathname.includes(path));
};

const redirectToLogin = () => {
    window.location.href = './login.html';
};

const token = storage.get(loginKey);

if (!token && !isPublicPage()) {
    redirectToLogin();
}


const errorLog = (type, msg) => {
    const user = storage.get(loginKey) && JSON.parse(storage.get(loginKey)) && Object.keys(JSON.parse(storage.get(loginKey))).length ? JSON.parse(storage.get(loginKey)) : { id: '123321' }
    axios.post(`${api}/log`, {
        type,
        userId: user.id,
        msg: typeof msg === "string" ? msg : JSON.stringify(msg),
    }).then(response => {
        console.log('日志已发送：', response.data);
    }).catch(error => {
        console.error('日志发送失败：', error);
    });
};

function isValidEmail(email) {
    // 正则表达式用于匹配电子邮件地址
    const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;

    // 测试传入的email是否匹配正则表达式
    return emailRegex.test(email);
}